﻿package cn.flashk.map.player
{
    import cn.flashk.map.*;
    import flash.events.*;

    public class PlayerSelf extends Player
    {
        private static var ins:PlayerSelf;

        public function PlayerSelf()
        {
            ins = this;
            this.addEventListener(Event.ENTER_FRAME, checkKeyMove);
            return;
        }// end function

        public function move(sx:Number, sy:Number) : void
        {
            this.x = this.x + sx;
            this.y = this.y + sy;
            if (this.x < 20)
            {
                this.x = 20;
            }
            if (this.x > MapEngine.getInstance().mapWidth - 26)
            {
                this.x = MapEngine.getInstance().mapWidth - 26;
            }
            if (this.y < 0)
            {
                this.y = 0;
            }
            if (this.y > MapEngine.getInstance().mapHeight - 63)
            {
                this.y = MapEngine.getInstance().mapHeight - 63;
            }
            this.x = int(this.x);
            this.y = int(this.y);
            MapEngine.getInstance().moveTo(this.x, this.y);
            return;
        }// end function

        public function moveTo(px:Number, py:Number) : void
        {
            MapEngine.getInstance().moveTo(px, py);
            this.x = px;
            this.y = py;
            return;
        }// end function

        private function checkKeyMove(event:Event) : void
        {
            if (MapEngine.getInstance().keys[1] == true)
            {
                move(-3, 0);
                this.scaleX = -1;
            }
            if (MapEngine.getInstance().keys[2] == true)
            {
                move(3, 0);
                this.scaleX = 1;
            }
            if (MapEngine.getInstance().keys[3] == true)
            {
                move(0, -3);
            }
            if (MapEngine.getInstance().keys[4] == true)
            {
                move(0, 3);
            }
            return;
        }// end function

        public static function getInstance() : PlayerSelf
        {
            return ins;
        }// end function

    }
}
